<template>
  <div class="mod-role">
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%"
    >
      <el-table-column
        prop="settingId"
        header-align="center"
        align="center"
        width="80"
        label="功能ID"
      >
      </el-table-column>
      <el-table-column
        prop="settingName"
        header-align="center"
        align="center"
        label="功能名"
      >
      </el-table-column>
      <el-table-column label="是否启用">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enabled"
            :active-value="true"
            :inactive-value="false"
            active-color="#02538C"
            inactive-color="#B9B9B9"
            @change="changeSwitch(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data () {
    return {
      entId: -1,
      dataList: [],
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.entId = this.$route.query.entId
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/app/ent/settings/info/' + this.entId),
        method: 'get'
      }).then(({ data }) => {
        if (data && data.code === 200) {
          this.dataList = data.settings
        } else {
          this.dataList = []
        }
        this.dataListLoading = false
      })
    },
    changeSwitch (row) {
      const req = {
        entId: this.entId,
        settingId: row.settingId,
        enabled: row.enabled
      }
      this.$http({
        url: this.$http.adornUrl(`/sys/ent/settings/save`),
        method: 'post',
        data: this.$http.adornData(req)
      }).then(({data}) => {
        if (data && data.code === 200) {
          this.$message({
            message: '更新成功',
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.visible = false
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      })
    }
  }
}
</script>
